package com.infore.dataReceiving.conf;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.Trigger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.expression.ParseException;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import com.infore.dataReceiving.quartz.Air212ScheduledTasks;
import com.infore.dataReceiving.quartz.JobFactory;

//@Configuration
public class Air212QuartzConfig {
	
	@Autowired
    private JobFactory jobFactory;
	
	// 配置中设定了
    // ① targetMethod: 指定需要定时执行scheduleInfoAction中的simpleJobTest()方法
    // ② concurrent：对于相同的JobDetail，当指定多个Trigger时, 很可能第一个job完成之前，
    // 第二个job就开始了。指定concurrent设为false，多个job不会并发运行，第二个job将不会在第一个job完成之前开始。
    // ③ cronExpression：0/10 * * * * ?表示每10秒执行一次，具体可参考附表。
    // ④ triggers：通过再添加其他的ref元素可在list中放置多个触发器。 scheduleInfoAction中的simpleJobTest()方法
	
    @Bean(name = "GetAirRealTimeDataJobDetail")
    public MethodInvokingJobDetailFactoryBean GetAirRealTimeDataJobDetail(Air212ScheduledTasks scheduledTasks){
        MethodInvokingJobDetailFactoryBean bean = new MethodInvokingJobDetailFactoryBean ();
        bean.setTargetObject (scheduledTasks);
        bean.setTargetMethod ("GetAirRealTimeData");
        bean.setConcurrent (false);
        return bean;
    }

    @Bean(name = "GetAirRealTimeDataTrigger")
    public CronTriggerFactoryBean GetAirRealTimeDataTrigger(JobDetail GetAirRealTimeDataJobDetail){
        CronTriggerFactoryBean tigger = new CronTriggerFactoryBean ();
        tigger.setJobDetail (GetAirRealTimeDataJobDetail);
        try {
            tigger.setCronExpression ("0 0/2 * * * ? ");//每2分钟执行一次
        } catch (ParseException e) {
            e.printStackTrace ();
        }
        return tigger;

    }
    
    
    @Bean(name = "GetAirBackDataJobDetail")
    public MethodInvokingJobDetailFactoryBean GetAirBackDataJobDetail(Air212ScheduledTasks scheduledTasks){
        MethodInvokingJobDetailFactoryBean bean = new MethodInvokingJobDetailFactoryBean ();
        bean.setTargetObject (scheduledTasks);
        bean.setTargetMethod ("GetAirBackData");
        bean.setConcurrent (false);
        return bean;
    }

    @Bean(name = "GetAirBackDataTrigger")
    public CronTriggerFactoryBean GetAirBackDataTrigger(JobDetail GetAirBackDataJobDetail){
        CronTriggerFactoryBean tigger = new CronTriggerFactoryBean ();
        tigger.setJobDetail (GetAirBackDataJobDetail);
        try {
            tigger.setCronExpression ("0 0/2 * * * ? ");//每2分钟执行一次
        } catch (ParseException e) {
            e.printStackTrace ();
        }
        return tigger;

    }
    
    
    @Bean(name = "AddJobDetail")
    public MethodInvokingJobDetailFactoryBean AddJobDetail(Air212ScheduledTasks scheduledTasks){
        MethodInvokingJobDetailFactoryBean bean = new MethodInvokingJobDetailFactoryBean ();
        bean.setTargetObject (scheduledTasks);
        bean.setTargetMethod ("AddJob");
        bean.setConcurrent (false);
        return bean;
    }

    @Bean(name = "AddJobTrigger")
    public CronTriggerFactoryBean StartJobTrigger(JobDetail AddJobDetail){
        CronTriggerFactoryBean tigger = new CronTriggerFactoryBean ();
        tigger.setJobDetail (AddJobDetail);
        try {
            tigger.setCronExpression ("0 0/1 * * * ? ");//每1minute执行一次
        } catch (ParseException e) {
            e.printStackTrace ();
        }
        return tigger;

    }
    
    
    @Bean(name = "RomoteDataDetail")
    public MethodInvokingJobDetailFactoryBean RomoteDataDetail(Air212ScheduledTasks scheduledTasks){
        MethodInvokingJobDetailFactoryBean bean = new MethodInvokingJobDetailFactoryBean ();
        bean.setTargetObject (scheduledTasks);
        bean.setTargetMethod ("RomoteData");
        bean.setConcurrent (false);
        return bean;
    }

    @Bean(name = "RomoteDataTrigger")
    public CronTriggerFactoryBean RomoteDataTrigger(JobDetail RomoteDataDetail){
        CronTriggerFactoryBean tigger = new CronTriggerFactoryBean ();
        tigger.setJobDetail (RomoteDataDetail);
        try {
            tigger.setCronExpression ("0/30 * * * * ? ");//每30S执行一次
        } catch (ParseException e) {
            e.printStackTrace ();
        }
        return tigger;

    }
   
    
    @Bean(name = "schedulerFactory")
    public SchedulerFactoryBean schedulerFactory(Trigger GetAirRealTimeDataTrigger,Trigger GetAirBackDataTrigger,
    		Trigger AddJobTrigger,Trigger RomoteDataTrigger){
        SchedulerFactoryBean bean = new SchedulerFactoryBean ();
        bean.setTriggers (GetAirRealTimeDataTrigger,AddJobTrigger,RomoteDataTrigger);
        bean.setJobFactory(jobFactory);
        return bean;
    }
    
    @Bean(name = "scheduler")
    public Scheduler scheduler(SchedulerFactoryBean schedulerFactory) {
        return schedulerFactory.getScheduler();
    }



}
